// pages/mall/index/index.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
let http = new Http()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [],
    goods: [],
    coupons: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userToken = wx.getStorageSync("userToken")
    if (userToken != undefined && userToken.length > 0) {
      getApp().globalData.userToken = userToken
    }
    this.setData({
      pageNum: 1,
      pageSize: 6,
      pages: 1,
      goods: [],
      bannersCurrent: 0
    })
    this.getMallBanner()
    this.getAllGoods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllCoupons()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.onLoad()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getAllGoods()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 自定义方法
   */
  getMallBanner: function () {
    var that = this
    http.sendHttp(Api.get_mall_banner, {
      data: {},
      success: function (res) {
        that.setData({
          banners: res.data.obj.banners
        })
      }
    })
  },

  getAllGoods: function () {
    var that = this
    var pageNum = this.data.pageNum
    var pageSize = this.data.pageSize
    var pages = this.data.pages
    if (pageNum <= pages) {
      http.sendHttp(Api.get_all_goods, {
        data: {
          pageNum: pageNum,
          pageSize: pageSize
        },
        success: function (res) {
          that.setData({
            goods: that.data.goods.concat(res.data.obj.goods),
            pages: res.data.obj.pages,
            pageNum: pageNum + 1
          })
        }
      })
    }
  },

  getAllCoupons: function () {
    var that = this
    http.sendHttp(Api.get_all_coupons, {
      data: { },
      success: function (res) {
        that.setData({
          coupons: res.data.obj.coupons
        })
      }
    })
  },

  receiveCoupon: function (e) {
    var couponId = e.detail.couponId
    var that = this
    http.sendHttp(Api.receive_coupon, {
      data: {
        couponId: couponId
      },
      success: function (res) {
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
      },
      finish: function () {
        that.getAllCoupons()
      }
    }, 'POST')
  },

  goSearch: function () {
    wx.navigateTo({
      url: '/pages/mall/search/search',
    })
  },

  goPage: function (e) {
    var index = e.currentTarget.dataset.index
    var banner = this.data.banners[index]
    if (banner.linkType == 1) {
      wx.navigateTo({
        url: banner.link,
      })
    } else if (banner.linkType == 2) {
      wx.navigateTo({
        url: '/pages/mall/bannerLink/bannerLink?link=' + banner.link,
      })
    }
  }
 })